/*
 * ================================================================================变量定义区
 */
var showCount = 10; //定义每页显示条数
var currentPage = 1;//全局变量，当前页
var page_index = 0;//页面索引
var chooseIds = new Array;
var vdinames = new Array(); 
var choosePopIds = new Array;
/*
 * ================================================================================页面初始化函数
 */
$(function()
{  
	
	//获取页面类型
	var pageType = $("#pageType").val();
	//获取上次列表
	if(pageType === 'lastest')
	{
		getLastestListAndForm();
	}
	else
	{
		getDataList1(currentPage);
	}
	
	//绑定回车事件
	document.onkeydown = function(e)
	{ 
	    var ev = document.all ? window.event : e;
		    
	    if(ev.keyCode==13) 
	    {
	    	queryVdiSysInfoDataByName();
	    }
	};
});
/*
 * ================================================================================函数定义区
 * 
 */

/**
 * 取消按钮事件
 */
function cancel()
{
	 window.close();
}

/**
 * 动态创建span
 */ 
function createSpan(o)
{  
	
var id = $(o).attr("id"); 
var $vdinameTd = $("#"+id).find("td:first");
var vdi_id = $(o).find("td[name='vdi_id']").attr("value");
var vdiname = $vdinameTd.text();

	    if(vdinames.length==0)
	    {
	    	vdinames.push(vdiname);
	    	choosePopIds.push(vdi_id);
	    }
	    else
	    {
		    for(var i=0;i<vdinames.length;i++)
		    {
		    	if(vdinames[i] == vdiname)
		    	{
		    		return;
		    	}
	        }
		    
		    vdinames.push(vdiname);
		    choosePopIds.push(vdi_id);
	    } 
	  
	var spanTag = document.createElement("span"); 
    spanTag.className = "dynamicSpan";    
    spanTag.innerHTML =  vdiname + " ";  
    var oImg = document.createElement('img');   
    oImg.src = "../res/img/item_delete.jpg";    
	
	
    oImg.onclick=
    function()
    {   
    	for(var i=0;i<vdinames.length;i++)
    	{
	    	if(vdinames[i]==vdiname)
	    	{ 
	    		vdinames.splice(i,1);
	    		choosePopIds = deleteArrayByIndex(i,choosePopIds);
		    }
    	} 
    	
    	document.getElementById('selected').removeChild(spanTag);  
    };
    
    spanTag.appendChild(oImg); 
    
    spanTag.ondblclick=
	function()
	{   
    	for(var i=0;i<vdinames.length;i++)
    	{
	    	if(vdinames[i]==vdiname)
	    	{ 
    			vdinames.splice(i,1);
    			choosePopIds = deleteArrayByIndex(i,choosePopIds);
		    }
    	} 
    	
    	document.getElementById('selected').removeChild(spanTag);   
    };
    
    document.getElementById('selected').appendChild(spanTag); 
}


/**
 * 弹出框
 * 
 * 
 */
function VdicallBackButton()
{
		window.parent.opener.vdiArray = getSelectedRowsPopup();
		window.close();
}


/**
 * 获取选中的系统信息
 */ 
function getSelectedRowsPopup()
{
	var vdiArray=new Array();
	vdiArray[0]=new Array();
	
	//return chooseIds;
	if(chooseId)
		return chooseId;
	
	vdiArray[0][0]=choosePopIds;
	vdiArray[0][1]=vdinames;
	
	return vdiArray;
}



/**
 * 获取列表数据
 * @param currentPage 请求的页码
 */
function getDataList1(pageIndex)
{
    var type = $("#type").val();
    var username = $("#username").val();
    
    SessionStorage.setSearchParam(
    {
    	searchParam: {
    		showCount:showCount,
    		currentPage:pageIndex,
    		type:type,
    		username:username
    	}
    });
    
	$.ajax(
	{
		type: 'POST',
        url: BASE_PATH +'/vdiSystemInfo/getVdiSystemInfoList.do',
        dataType:"json",
        data:
        {
        	"showCount":showCount,
        	"currentPage":pageIndex,
        	"type":type,
        	"username":username,
        	"query_condition_field_list":"type,username"
        },
        cache:false,
        success:function(data,textStatus)
        {
        	var html = "";        	
        	var pageData = data;
        	var totalResult = pageData.queryParam.totalResult;
        	
        	if(pageData.queryResult != null)
        	{
        		//解析data, 组装成html
	        	$.each(pageData.queryResult,function(i,n)
	        	{
	        		html += '<tr id="tr_index_' + i + '" '+ 'ondblclick="createSpan(this);" '+'>'
        		
        			     + '<td title="' + switcHtml(n.name) + '">' + switcHtml(n.name) + '</td>';
        		
	        		html += '</td>'
		        		+ '<td style="width:220px;" title="'+switcHtml(n.remark)+'">' + switcHtml(n.remark) + '</td>'
		        		+ '<td style="width:220px;" name="vdi_id"  value=' + switcHtml(n.id) + ' hidden="hidden"'+'>' + switcHtml(n.id) + '</td>'
		        		+'<td title=' + n.createUser + '>' + n.createUser + '</td>'	
		        		 + '<td style="text-align:center;" title=' + n.createTime + '>' + n.createTime + '</td>'
		        		 + '<td style="text-align:center;" title=';
        			
        			if(n.lastModifyTime == null)
        			{
        				html += "";
        			}
        			else 
        			{
        				html += n.lastModifyTime;
        			}
        			
        			html += '>'+n.lastModifyTime;
	        		html += '</td>'+'<td title=' + n.lastModifyUser + '>' + n.lastModifyUser + '</td>'+'<td style="width: 1px;"></td></tr>';
	        	}); 
        	} 
        	else
        	{
        		html = '<tr><td colspan="2">No items.</td></tr>';
        	}
        	
        	$('#tBodyList').html(html);
        	currentPage = pageIndex;
        	
    		if(totalResult == 0)
    		{
    			$('#tBodyList').html('<tr><td style="text-align:center;">No items.</td></tr>');
    			$("#pagination").hide();
    		} 
    		else 
    		{
    			// 创建分页
    			$("#pagination").show();
    			$("#pagination").pagination(totalResult, 
    			{
        			num_edge_entries: 1, //边缘页数
        			num_display_entries: 3, //主体页数
        			current_page:currentPage,
        			callback: getDataList1,
        			items_per_page:showCount //每页显示1项
    			});
    		};
    		
    		chooseId= null;
    		
        },
		error:function(e)
		{
			 openAlert("加载数据错误！"); 
			 return;
		}
	});
} 


/**
 * 点击查询事件
 */ 
function queryDataVdi()
{
	chooseId = null;
	queryVdiSysInfoDataByName(1);
}


/**
 * 获取列表数据
 */ 
function queryVdiSysInfoDataByName(pageIndex)
{
	 var username = $("#usernamepermisson").val();
	 var vdiSysName = $("#vdiSysName").val().toUpperCase();
	 
	 $.ajax(
		{
			type: 'POST',
	        url: BASE_PATH +'/vdiSystemInfo/queryVdiSysInfoDataByName.do',
	        dataType:"json",
	        data:
	        {
	        	"showCount":showCount,
	        	"currentPage":pageIndex,
	        	"vdiName":vdiSysName,
	        	"username":username,
	        	"query_condition_field_list":"vdiName,username"
	        },
	        cache:false,
	        success:function(data,textStatus)
	        {
	        	var html = "";        	
	        	var pageData = data;
	        	var totalResult = pageData.queryParam.totalResult;
	        	
	        	if(pageData.queryResult != null && totalResult>0)
	        	{
	        		//解析data, 组装成html
		        	$.each(pageData.queryResult,function(i,n)
		        	{
		        		html += '<tr id="tr_index_' + i + '" '+ 'ondblclick="createSpan(this);"'+'>'
	        		
	        			     + '<td title="' + switcHtml(n.name) + '">' + switcHtml(n.name) + '</td>';
	        		
		        		html += '</td>'
			        		+ '<td style="width:220px;" title="'+switcHtml(n.remark)+'">' + switcHtml(n.remark) + '</td>'
			        		+ '<td style="width:220px;" name="vdi_id"  value=' + switcHtml(n.id) + ' hidden="hidden"'+'>' + switcHtml(n.id) + '</td>'
			        		+'<td title=' + n.createUser + '>' + n.createUser + '</td>'	
			        		 + '<td style="text-align:center;" title=' + n.createTime + '>' + n.createTime + '</td>'
			        		 + '<td style="text-align:center;" title=';
	        			
	        			if(n.lastModifyTime == null)
	        			{
	        				html += "";
	        			}
	        			else 
	        			{
	        				html += n.lastModifyTime;
	        			}
	        			
	        			html += '>'+n.lastModifyTime;
		        		html += '</td>'+'<td title=' + n.lastModifyUser + '>' + n.lastModifyUser + '</td>'+'<td style="width: 1px;"></td></tr>';
		        		
		        	}); 
	        	} 
	        	else
	        	{
	        		html = '<tr><td colspan="2" style="text-align:center;">No items.</td></tr>';
	        		$("#pagination").hide();
	        	}
	        	
	        	$('#tBodyList').html(html);
	        	currentPage = pageIndex;
	        	
	    		if(totalResult == 0)
	    		{
	    			$('#tBodyList').html('<tr><td style="text-align:center;">No items.</td></tr>');
	    			$("#pagination").hide();
	    		} 
	    		else 
	    		{
	    			// 创建分页
	    			$("#pagination").show();
	    			$("#pagination").pagination(totalResult, 
	    			{
	    				num_edge_entries: 1, //边缘页数
	    				num_display_entries: 3, //主体页数
	    				current_page:currentPage,
	    				callback: queryVdiSysInfoDataByName,
	    				items_per_page:showCount //每页显示1项
	    			});
	    		}
	    		
	    		
	        },
			error:function(e)
			{
				 openAlert("加载数据错误！"); 
				 return;
			}
		});
}

/**
 * 获取最近一次的列表
 * 从SessionStorage中获取参数
 */
function getLastestListAndForm()
{
	//获取参数
	var param = SessionStorage.getSearchParam(),
	//搜索参数
	searchParam = param.searchParam;

	getDataList1(searchParam.currentPage);
	
	//向SessionStorage存储数据
	SessionStorage.setSearchParam(
	{
		searchParam: searchParam
	});
}


